package com.daxia.bumall.web.controller.admin;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.daxia.bumall.dto.JsonResultDTO;
import com.daxia.bumall.dto.ProductDTO;
import com.daxia.bumall.dto.SpecificationDTO;
import com.daxia.bumall.service.ProductService;
import com.daxia.bumall.service.SpecificationService;
import com.daxia.bumall.support.Page;
import com.daxia.bumall.web.controller.BaseController;

@Controller
@RequestMapping(value = "/admin/specification", produces="text/html;charset=UTF-8")
public class AdminSpecificationController extends BaseController {

	@Autowired
	private SpecificationService specificationService;
	@Autowired
	private ProductService productService;

	@RequestMapping(value = "/detail")
	public String detail(Map<String, Object> map, Long id, Long pId) {
		ProductDTO p = productService.load(pId);
		map.put("p", p);
		if (id != null) {
			SpecificationDTO dto = specificationService.load(id);
			map.put("n", dto);
		}
		return "admin/specification/specification_detail";
	}
	
	@ResponseBody
	@RequestMapping(value = "/save")
	public String save(SpecificationDTO dto, JsonResultDTO resultDTO) throws Exception {
		if (dto.getId() == null) {
			ProductDTO productDTO = productService.load(dto.getProduct().getId());
			dto.setProduct(productDTO);
			Integer seq = specificationService.findSeq(dto);
			synchronized(seq){
				dto.setSeq(seq + 1); 
				specificationService.create(dto);
			}
			eventDispather.specificationChanged(productDTO.getId());
		} else {
			SpecificationDTO old = specificationService.load(dto.getId());
			old.setOgCode(dto.getOgCode());
			old.setFactoryCode(dto.getFactoryCode());
			old.setSpec1(dto.getSpec1());
			old.setSpec2(dto.getSpec2());
			old.setSellingPrice(dto.getSellingPrice());
			old.setWeight(dto.getWeight());
			old.setSize(dto.getSize());
			old.setStock(dto.getStock());
			old.setImages(dto.getImages());
			specificationService.updateAllFields(old);
			eventDispather.specificationChanged(old.getProduct().getId());
		}
		return okAndClose(resultDTO);
	}

	@ResponseBody
	@RequestMapping(value = "/delete") 
	public String delete(Long[] ids, JsonResultDTO resultDTO) throws IOException {
		specificationService.deleteByIds(ids);
		return okAndRefresh(resultDTO);
	}
	
	@RequestMapping(value = "/list")
	public String list(SpecificationDTO dto, Map<String, Object> map, Page page) {
		List<SpecificationDTO> dtos = specificationService.find(dto, page);
		map.put("list", dtos);
		map.put("query", dto);
		if (dto.getProduct() != null && dto.getProduct().getId() != null) {
			map.put("pId", dto.getProduct().getId());
		}
		return "admin/specification/specification_list";
	}
	
	@ResponseBody
	@RequestMapping(value = "/datagrid")
    public String datagrid(SpecificationDTO dto, Map<String, Object> map, Page page) throws Exception {
        List<SpecificationDTO> dtos = specificationService.find(dto, page);
        JSONArray jsonArray = new JSONArray();
        for (SpecificationDTO specification : dtos) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", specification.getId());
	        jsonObject.put("product.name", specification.getProduct().getName());    	
            jsonObject.put("factoryCode", specification.getFactoryCode());
            jsonObject.put("spec1", specification.getSpec1());
            jsonObject.put("spec2", specification.getSpec2());
            jsonObject.put("sellingPrice", specification.getSellingPrice());
            jsonObject.put("marketPrice", specification.getMarketPrice());
            jsonObject.put("weight", specification.getWeight());
            jsonObject.put("size", specification.getSize());
            jsonObject.put("stock", specification.getStock());
            jsonArray.add(jsonObject);
        }
        JSONObject json = new JSONObject();
        json.put("total", page.getTotalRecords());
        json.put("rows", jsonArray);
        return json.toJSONString();
    }
}
